package cn.springcloud.fix.common.core.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

@Slf4j
public class AddressUtil {

    private static final String IP_URL = "http://region.zmrit.com";

    public static String getRealAddressByIP(String ip) {
        try {
            String address = "unknown";
            String rspStr = HttpUtil.sendPost(IP_URL, "ip=" + ip);
            if (StringUtils.isEmpty(rspStr)) {
                log.error("获取地理位置异常 {}", ip);
                return address;
            }
            JSONObject data = JSON.parseObject(rspStr).getJSONObject("data");
            return data.getString("address");
        } catch (Exception e) {
            log.error("获取地理位置异常:" + ip, e);
            return null;
        }
    }
}
